import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {path: '/',redirect:'/home/homepage'},
    {path: '/admin',redirect:'/admin/backstagehome'},
    {path:'/home',name:'home',meta:{title:'主页'},component: () => import('../views/Home.vue'),children:[
      {path: 'enterprise',name: 'enterprise',meta:{title:'企业页面'},component: () => import('../views/enterprise/Enterprise.vue'),children:[
        {path: 'enterprisehome',name: 'enterprisehome',meta:{title:'首页'},component: () => import('../views/enterprise/EnterpriseHome.vue'),},
        {path: 'payendowment',name: 'payendowment',meta:{title:'缴纳保险'},component: () => import('../views/enterprise/PayEndowment.vue'),},
        {path: 'enterpriseemplyeeinfo',name: 'enterpriseemplyeeinfo',meta:{title:'员工信息'},component: () => import('../views/enterprise/EnterpriseEmplyeeInfo.vue'),},
        {path: 'enterpriseendowment',name: 'enterpriseendowment',meta:{title:'缴纳记录'},component: () => import('../views/enterprise/EnterpriseEndowment.vue'),},
        {path: 'retireemplyee',name: 'retireemplyee',meta:{title:'退休员工'},component: () => import('../views/enterprise/RetireEmplyee.vue'),},
      ]},
      {path: 'calculator',name: 'calculator',meta:{title:'问题咨询'},component: () => import('../views/calculator/Calculator.vue'),children:[
        {path: 'retireAge',name: 'retireAge',meta:{title:'退休年龄计算器'},component: () => import('../views/calculator/retireAge.vue'),},
        {path: 'retireMoney',name: 'retireMoney',meta:{title:'退休金计算器'},component: () => import('../views/calculator/retireMoney.vue'),},
      ]},
      {path: 'policy',name: 'policy',meta:{title:'政策宣传'},component: () => import('../views/Policy.vue'),children:[
      ]},
      {path: 'detail/:id',name:"detail",meta:{title:'详情'},component: () => import('../views/NoticeDetail.vue'),},
      {path: 'homepage',name: 'homepage',meta:{title:'首页'},component: () => import('../views/HomePage.vue'),},
      ]
    },
    {path: '/admin',name: 'admin',meta:{title:'后台管理页面'},component: () => import('../views/admin/Admin.vue'),children:[
      {path:'enterpriseManage',name:'enterpriseManage',meta:{title:'企业管理'},component:()=>import('../views/admin/EnterpriseManage.vue')},
      {path:'backstagehome',name:'backstagehome',meta:{title:'后台首页'},component:()=>import('../views/admin/BackStageHome.vue')},
      {path:'webmanagement',name:'webmanagement',meta:{title:'网页管理'},component:()=>import('../views/admin/WebManagement.vue')},
      {path:'manageendowment',name:'manageendowment',meta:{title:'缴纳记录'},component:()=>import('../views/admin/ManageEndowment.vue')},
      {path:'adminmanager',name:'adminmanager',meta:{title:'管理员信息'},component:()=>import('../views/admin/AdminManager.vue')},
    ]},
    {path:'/404',name:'NotFound',meta:{title:'404找不到页面'},component:()=>import('../views/404.vue')},
    {path:'/register',name:'register',meta:{title:'企业注册'},component:()=>import('../views/register.vue')},
    {path:'/login',name:'login',meta:{title:'登录'},component:()=>import('../views/Login.vue')},
    {path:'/:pathMatch(.*)',redirect:'/404'},
    {path:'/test',name:'test',meta:{title:'测试'},component:()=>import('../components/test.vue')},
  ],
})

// 路由守卫
router.beforeEach((to, from, next) => {
  document.title = to.meta.title; // 设置页面标题
  const token = localStorage.getItem('token')
  const enterprise = localStorage.getItem('enterprise')
  const admin = localStorage.getItem('admin')

  // 判断是不是访问 enterprise 区域
  if (to.path.startsWith('/home')) {
    if (!token || !enterprise) {
      ElMessage.error('请先登录企业账户')
      next('/login')
      return
    }
  }
  // 判断是不是访问 admin 区域
  if (to.path.startsWith('/admin')) {
    if (!token || !admin) {
      ElMessage.error('请先登录管理员账户')
      next('/login')
      return
    }
  }
    // 否则允许跳转
    next();
});
export default router
